package org.jaaksi.test.file;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

public class MyActivity extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		initFiles();
		File dir = new File(Environment.getExternalStorageDirectory().toString()
		        + File.separator + "homelink");
		File file = new File(dir, "0.log");
		try
		{
			PrintStream printStream = new PrintStream(file);
			printStream.println("这只是个测试1");
			printStream.println("这只是个测试2");
			printStream.println("这只是个测试3");
			printStream.close();
            printStream = new PrintStream(new File(dir,"1.log"));
			printStream.println("这只是个测试4");
            printStream.close();

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}

	}

	private static boolean isSdCardAvailable()
	{
		File file = Environment.getExternalStorageDirectory();
		return Environment.getExternalStorageState().equals(
		        Environment.MEDIA_MOUNTED) && (file != null && file.exists());
	}

	/**
	 * 初始化创建5个文件作为缓冲，分别为0,1,2,3,4 .log
	 */
	public static void initFiles()
	{
		if (isSdCardAvailable())
		{
			// 在内存卡根目录创建homelink文件夹
			File dir = new File(
			        Environment.getExternalStorageDirectory().getPath()
			                + File.separator + "homelink");
			if (!dir.exists())
			{
				dir.mkdir();
			}

			for (int i = 0; i < 5; i++)
			{
				File logFile = new File(dir, i + ".log");
				if (!logFile.exists())
				{
					try
					{
						logFile.createNewFile();
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				}
			}

		}
	}
}
